#include <cstdlib>
#include <iostream>

/*
new:先调用operator new开辟内存空间，然后调用对应的构造函数
delete：先调用对象的析构函数，然后调用operator delete释放内存空间
*/

#if 0
void* operator new(size_t size) {
    void* p = malloc(size);
    std::cout << "operator new:" << p << std::endl;
    if (p == nullptr) throw std::bad_alloc();
    return p;
}

void operator delete(void* ptr) {
    std::cout << "operator delete:" << ptr << std::endl;
    free(ptr);
}

void* operator new[](size_t size) {
    void* p = malloc(size);
    std::cout << "operator new[]:" << p << std::endl;
    if (p == nullptr) throw std::bad_alloc();
    return p;
}

void operator delete[](void* ptr) {
    std::cout << "operator delete[]:" << ptr << std::endl;
    free(ptr);
}

#endif

int main() {
    int* p = new int(3);
    std::cout << p << std::endl;
    delete p;

    int* p2 = new int[4];
    std::cout << p2 << std::endl;
    delete[] p2;
}
